package io.renren.modules.vastFinance.controller;

import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.alibaba.excel.EasyExcel;
import io.renren.common.utils.Result;
import com.tencent.common.pojo.StatusCode;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.vastFinance.entity.FinanceEntity;
import io.renren.modules.vastFinance.service.FinanceService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;


/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2022-05-23 12:11:18
 */
@RestController
@RequestMapping("vastFinance/finance")
public class FinanceController {
    @Autowired
    private FinanceService financeService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("vastFinance:finance:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = financeService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{finId}")
    @RequiresPermissions("vastFinance:finance:info")
    public R info(@PathVariable("finId") Integer finId){
		FinanceEntity finance = financeService.getById(finId);

        return R.ok().put("finance", finance);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("vastFinance:finance:save")
    public R save(@RequestBody FinanceEntity finance){
		financeService.save(finance);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("vastFinance:finance:update")
    public R update(@RequestBody FinanceEntity finance){
		financeService.updateById(finance);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("vastFinance:finance:delete")
    public R delete(@RequestBody Integer[] finIds){
		financeService.removeByIds(Arrays.asList(finIds));

        return R.ok();
    }

    /**
     * 根据上级Id查询子节点数据
     * @param parentId
     * @return
     */
    @RequestMapping("/getAll")
    public Result getAllList(@RequestParam("parentId") Integer parentId){
      List<FinanceEntity> list= financeService.getAllListByPid(parentId);
     return  new Result(StatusCode.OK,"查询财务表数据成功",true,list);
    }

    /**
     * 财务报表导入数据
     * @param file
     * @return
     */
    @RequestMapping("/upload")
    public Result importData(@RequestParam("file") MultipartFile file){
        try {
            InputStream inputStream = file.getInputStream();
            financeService.importData(inputStream);
            return new Result(StatusCode.OK,"数据导入成功",true);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(StatusCode.ERROR,"数据导入失败",false);
    }

    /**
     * 文件下载（失败了会返回一个有部分数据的Excel）
     * <p>
     * <p>
     * 2. 设置返回的 参数
     * <p>
     * 3. 直接写，这里注意，finish的时候会自动关闭OutputStream,当然你外面再关闭流问题不大
     */
    @GetMapping("download")
    public void download(HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("myFinance", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(),FinanceEntity.class).sheet("sheet1")
                .doWrite(financeService.financeData());
    }


}
